var androidUrl = '';
var appPublishUrl = 'mrht://invokeApp?0W+q/wK1jek0rrYg32NCyfeJ6BBe65KkBKoQi7HEqOZJ+PtyJ3/bUIZhJhSD0oMI3SClqcRT9L3ce3of6kJk/KrykYZ+uwmW85WC0+P9JCNSREWVQmBB55EldYfWBs9rYuI7c2L9OdPS5rddv3YHC8ENqp+obcpOLte0cx7PRI0=';
var appOpenUrl = 'mrht://invokeApp';

//浏览器类型判断
function browserType(){
    var userAgent=navigator.userAgent.toLowerCase();
    if(userAgent.indexOf('android')>-1&& userAgent.indexOf('linux')>-1){
        return 'android';
    }else if(userAgent.indexOf('iphone')>-1){
        return 'iphone';
    }else{
        return false;
    }
}
function getUrlValue(name)//获取URL参数
{
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
    if(window.location.search.substr(1).match(reg))
    {
        arr=window.location.search.substr(1).match(reg);
        return decodeURIComponent(arr[2]);
    }else{
        return undefined;
    }
}

//微信提示
$('#wxTip').click(function(){
    $(this).hide();
});

//是否微信
function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

//打开APP
function openApp(){
    if(getUrlValue('type') == 'publish'){
        redirectUrl(appPublishUrl); //通过URL参数判断是否需要直接打开APP发布页面
    }else{
        redirectUrl(appOpenUrl); //直接尝试是否安装APP，并打开
    }
}

function redirectUrl(url){
    if(typeof url == 'undeinfed'){
        console.log('需要跳转的url为空');
        return false;
    }
    window.location.href = url;
}

window.onload = function(){
    document.body.className = 'body-bg active';
    var linkA = document.getElementById('download-btn');

    //页面加载时如果安装了APP，直接打开APP或APP对应页面
    openApp();
    //如果没安装APP，或者是安装了打不开继续往下走
    $.ajax({
        url : 'http://api.haitai.huo.so/hitgou/config?os=0',
        dataType : 'jsonp',
        success : function(json){
            if(json.status != 2000){
                return false;
            }
            //获取到安卓的下载地址
            androidUrl = json.data.version.url;
        }
    });
};


$('#download-btn').click(function(e){
    if(isWeiXin()){
        $('#wxTip').show();
        e.preventDefault();
    };
    if(browserType() == 'android'){
        openApp();
        setTimeout(function(){
            redirectUrl(androidUrl);
        },100);
    }else if(browserType() == 'iphone'){
        redirectUrl('https://itunes.apple.com/cn/app/hai-tai-jun-fa-xian-xin-shi-jie/id1107483884?mt=8');  //跳转APPSTORE
    }
});
